##    This library is free software; you can redistribute it and/or
##    modify it under the terms of the GNU Lesser General Public
##    License as published by the Free Software Foundation; either
##    version 2.1 of the License, or (at your option) any later version.
##
##    This library is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
##    Lesser General Public License for more details.
##
##    You should have received a copy of the GNU Lesser General Public
##    License along with this library; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import logging

# App Engine imports
from google.appengine.ext import webapp
from google.appengine.api import users

# OpenSocial Gifts imports
from db_model import Gift, GiftTransaction

logging.getLogger().setLevel(logging.INFO)

class Admin:
  """Initializes the list of gifts in the datastore."""

  def initGifts(self):
    """Deletes any existing gifts and add the default gifts."""
    for gift in Gift.all():
      gift.delete()
    GIFT_NAMES = ['a cashew nut', 
                  'a peanut', 
                  'a hazelnut', 
                  'a red pistachio nut']
    for name in GIFT_NAMES:
      gift = Gift()
      gift.name = name
      gift.put()

  def initGiftTransactions(self):
    """Deletes any existing gift transactions."""
    for t in GiftTransaction.all():
      t.delete()

  def getGiftNames(self):
    names = []
    for gift in Gift.all():
      names.append(gift.name)
    return names

  def getGiftTransactions(self):
    giftTransactions = []
    for t in GiftTransaction.all():
      giftTransactions.append("sender: %s, reciever: %s, gift: %s" % 
          (t.sender_id, t.receiver_id, t.gift.key()))
    return giftTransactions

class AdminServer(webapp.RequestHandler):
  """Handles requests to /admin URLs and delegates to the Admin class."""

  def test_rsa(self):
    import verify
    return verify.verify()

  def _handleRequest(self):
    """Invokes methods from the Admin class based on the 'action' parameter"""
    admin = Admin()
    action = self.request.get('action')
    if action == 'init':
      admin.initGifts()
      admin.initGiftTransactions()
      msg = "Gifts have been initialized, gift transactions have been cleared."
      self.response.out.write(msg)
    elif action == 'list':
      self.response.out.write("Gifts = %s" % admin.getGiftNames())
      self.response.out.write("<br>")
      self.response.out.write("Gfit Transactions = %s" % admin.getGiftTransactions())
    elif action == 'test_rsa':
      if self.test_rsa():
        self.response.out.write("Signature verified")
      else:
        self.response.out.write("Invalid signature")                
    else:
      html = []
      html.append('<a href="/admin?action=init">Initialize datastore</a><br>')
      html.append('<a href="/admin?action=list">List all data in datastore</a>')
      self.response.out.write(''.join(html))

  def get(self):
    """Ensure that the user is an admin, then invoke appropriate action."""
    if not users.GetCurrentUser():
      loginUrl = users.CreateLoginURL(self.request.uri)
      self.response.out.write('<a href="%s">Login</a>' % loginUrl)
      return

    if not users.IsCurrentUserAdmin():
      self.response.out.write('You must be an admin to view this page.')
      return

    self._handleRequest()
